<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

class Corplist extends Model
{
    protected $table = 'corp_list';
    protected $primaryKey = 'id';

    public static function getCorpList($openid)
    {
        $query = self::select("corp_id","name","avatar","collect","visitor_today","status","end_time")->where("openid",$openid)->where("status","!=",3);
        return $query->orderBy("id","DESC")->simplePaginate(10);
    }

    public static function corpListDetial($openid,$corp_id)
    {
        return self::where("openid",$openid)->where("corp_id",$corp_id)->first();
    }

    public static function corpListSample($corp_id)
    {
        return self::select("name","collect","examine","name")->where("corp_id",$corp_id)->first();
    }

    public static function corpListUpdate($openid,$corp_id,$name,$avatar,$collect,$examine,$notice)
    {
        return self::where("openid",$openid)->where("corp_id",$corp_id)->update(["name"=>$name,"avatar"=>$avatar,"avatar"=>$avatar,"collect"=>$collect,"examine"=>$examine,"notice"=>$notice]);
    }

    public static function corpListCreate($openid,$corp_id,$name,$avatar,$collect,$examine,$notice,$end_time)
    {
        return self::insertGetId(["openid"=>$openid,"name"=>$name,"corp_id"=>$corp_id,"avatar"=>$avatar,"collect"=>$collect,"examine"=>$examine,"notice"=>$notice,"end_time"=>$end_time,"created_at"=>time()]);
    }

    public static function corpListDel($openid,$corp_id)
    {
        return self::where("corp_id",$corp_id)->where("openid",$openid)->update(["status"=>3]);
    }

    public static function userCorpCount($openid)
    {
        return self::where("openid",$openid)->where("status",1)->count("id");
    }

    public static function corpListEndTime($openid,$end_time)
    {
        return self::where("openid",$openid)->update(["end_time"=>$end_time]);
    }
}